package com.whut.monitor.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.whut.monitor.service.IVideoService;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author CFlower
 * 2017/6/26
 * 远程摄像头服务层接口
 */
@Service
public class VideoServiceImpl implements IVideoService {

    @Override
    public JSONObject getAccessToken(String appKey, String appSecret) throws IOException {
        List<NameValuePair> formparams = new ArrayList<>();
        formparams.add(new BasicNameValuePair("appKey", appKey));
        formparams.add(new BasicNameValuePair("appSecret", appSecret));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
        HttpPost httppost = new HttpPost("https://open.ys7.com/api/lapp/token/get");
        httppost.setEntity(entity);

        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = httpclient.execute(httppost);
        HttpEntity httpEntity = response.getEntity();
        String respondStr = EntityUtils.toString(httpEntity, "utf-8");
        JSONObject j = JSONObject.parseObject(respondStr);
        j = JSONObject.parseObject(j.getString("data"));
        return j;
    }

    @Override
    public JSONObject getDeviceInfo(String accessToken, String deviceSerial) throws IOException {
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("accessToken", accessToken));
        formparams.add(new BasicNameValuePair("deviceSerial", deviceSerial));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
        HttpPost httppost = new HttpPost("https://open.ys7.com/api/lapp/device/info");
        httppost.setEntity(entity);

        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = httpclient.execute(httppost);
        HttpEntity httpEntity = response.getEntity();
        String respondStr = EntityUtils.toString(httpEntity, "utf-8");
        JSONObject j = JSONObject.parseObject(respondStr);
        j = JSONObject.parseObject(j.getString("data"));
        return j;
    }

    @Override
    public JSONObject setVideoControl(String accessToken, String deviceSerial, int channelNo, int direction, int speed) throws IOException {
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("accessToken", accessToken));
        formparams.add(new BasicNameValuePair("deviceSerial", deviceSerial));
        formparams.add(new BasicNameValuePair("channelNo", String.valueOf(channelNo)));
        formparams.add(new BasicNameValuePair("direction", String.valueOf(direction)));
        formparams.add(new BasicNameValuePair("speed", String.valueOf(speed)));

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
        HttpPost httppost = new HttpPost("https://open.ys7.com/api/lapp/device/ptz/start");
        httppost.setEntity(entity);

        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = httpclient.execute(httppost);
        HttpEntity httpEntity = response.getEntity();
        String respondStr = EntityUtils.toString(httpEntity, "utf-8");
        JSONObject j = JSONObject.parseObject(respondStr);
        return j;
    }

    @Override
    public JSONObject setVideoControlStop(String accessToken, String deviceSerial, int channelNo) throws IOException {
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("accessToken", accessToken));
        formparams.add(new BasicNameValuePair("deviceSerial", deviceSerial));
        formparams.add(new BasicNameValuePair("channelNo", String.valueOf(channelNo)));


        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
        HttpPost httppost = new HttpPost("https://open.ys7.com/api/lapp/device/ptz/stop");
        httppost.setEntity(entity);

        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = httpclient.execute(httppost);
        HttpEntity httpEntity = response.getEntity();
        String respondStr = EntityUtils.toString(httpEntity, "utf-8");
        JSONObject j = JSONObject.parseObject(respondStr);
        return j;
    }


}
